package ext.acae2.baseline.ui;

import java.rmi.RemoteException;
import java.util.Locale;

import org.apache.commons.lang.StringUtils;

import wt.enterprise.RevisionControlled;
import wt.fc.Persistable;
import wt.lifecycle.LifeCycleManaged;
import wt.type.TypedUtilityServiceHelper;
import wt.util.WTException;
import wt.util.WTMessage;

import com.ptc.core.ui.resources.FeedbackType;
import com.ptc.core.ui.validation.DefaultUIComponentValidator;
import com.ptc.core.ui.validation.UIValidationCriteria;
import com.ptc.core.ui.validation.UIValidationFeedbackMsg;
import com.ptc.core.ui.validation.UIValidationKey;
import com.ptc.core.ui.validation.UIValidationResult;
import com.ptc.core.ui.validation.UIValidationStatus;

import ext.acae2.baseline.resource.BaselineActionResource;
import ext.acae2.utilities.util.CSCUtil;


/**
 * @desription baseline member state check
 * @author cnie
 * @date 2013-3-8 下午9:55:23
 */
public class BaselineableStateValidator extends DefaultUIComponentValidator{
	
	
	private static final String RESOURCE = BaselineActionResource.class.getName();

	@Override
	public UIValidationResult validateFormSubmission(UIValidationKey uiValidationKey,
			UIValidationCriteria uivalidationCriteria, Locale local) throws WTException {
		// TODO Auto-generated method stub
		String addToBaseline_flag = uivalidationCriteria.getTextParameter("addToBaseline_flag");
		String[] selectedOidArr = null;
		if("addToBaseline".equals(addToBaseline_flag)){
			String selectedOid = uivalidationCriteria.getTextParameter("addRows_baselineMembersNPATable");
			selectedOidArr = selectedOid.split("#");
		}else if("addMembers".equals(addToBaseline_flag)){
			selectedOidArr = uivalidationCriteria.getTextParameterValues("pjl_selPJLsa1__1");
			for(int i=0;i<selectedOidArr.length;i++){
				String selectedOid = selectedOidArr[i];
				String oid = selectedOid.substring(selectedOid.lastIndexOf("$")+1,selectedOid.indexOf("!"));
				selectedOidArr[i]=oid;
			}
		}
		StringBuffer objnames = new StringBuffer();
		for(String selectedOid:selectedOidArr){
//			String oid = selectedOid.substring(selectedOid.lastIndexOf("$")+1,selectedOid.indexOf("!"));
			Persistable p = CSCUtil.getObjectByOid(selectedOid);
			
			try {
				String type = TypedUtilityServiceHelper.service.getExternalTypeIdentifier(p);
				if(p instanceof LifeCycleManaged){
					LifeCycleManaged lfm = (LifeCycleManaged)p;
					String stateDis = lfm.getState().getState().getDisplay();
					String lfmState = lfm.getState().toString();
					if(!"RELEASED".equals(lfmState)){
						RevisionControlled rc = (RevisionControlled)p;
						String name = rc.getName();
						objnames.append("\n");
						objnames.append(name);
					}
				}
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		UIValidationResult uivalidationresult = UIValidationResult.newInstance(uiValidationKey, UIValidationStatus.PERMITTED);
		if(StringUtils.isNotBlank(objnames.toString())){
			uivalidationresult.setStatus(UIValidationStatus.DENIED);
			String baselineWarn1 = WTMessage.getLocalizedMessage(RESOURCE, BaselineActionResource.BASELINEABLE_WARN1_INFO,null,local);
			String baselineWarn2 = WTMessage.getLocalizedMessage(RESOURCE, BaselineActionResource.BASELINEABLE_WARN2_INFO,null,local);
			uivalidationresult.addFeedbackMsg(UIValidationFeedbackMsg.newInstance(baselineWarn1+objnames.append("\n").toString()+baselineWarn2, FeedbackType.ERROR));
			return uivalidationresult;
		}else{
			return super.validateFormSubmission(uiValidationKey, uivalidationCriteria, local);
		}
		
		
	}


	
	

}
